"use strict";
cc._RF.push(module, '532bf4PuNlB1JBzbKW/RTLI', 'wxcloud');
// scripts/wxcloud.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.pukeManeger = exports.wxbase = exports.db = void 0;
var lodash = require("lodash");
var log = console.log;
var database = /** @class */ (function () {
    function database() {
    }
    database.prototype.collection = function (dir) {
        return new databasedir(dir);
    };
    return database;
}());
var databasedir = /** @class */ (function () {
    function databasedir(dir) {
        this.root = dir;
    }
    databasedir.prototype.get = function (id) {
        return JSON.parse(cc.sys.localStorage.getItem(this.root + "_" + id));
    };
    databasedir.prototype.remove = function (id) {
        cc.sys.localStorage.removeItem(this.root + "_" + id);
    };
    databasedir.prototype.add = function (data) {
        cc.sys.localStorage.setItem(this.root + "_" + data._id, JSON.stringify(data));
    };
    databasedir.prototype.doc = function (id) {
        return new databasefile(this.root + "_" + id);
    };
    return databasedir;
}());
var databasefile = /** @class */ (function () {
    function databasefile(filepath) {
        this.root = filepath;
    }
    databasefile.prototype.get = function () {
        return JSON.parse(cc.sys.localStorage.getItem(this.root));
    };
    databasefile.prototype.remove = function () {
        cc.sys.localStorage.removeItem(this.root);
    };
    databasefile.prototype.update = function (data) {
        cc.sys.localStorage.setItem(this.root, JSON.stringify(data));
    };
    return databasefile;
}());
exports.db = new database();
var wxbase = /** @class */ (function () {
    function wxbase(uid) {
        this.cloud = new wxcloud(uid);
    }
    wxbase.prototype.getId = function () {
        return this.cloud.uid;
    };
    return wxbase;
}());
exports.wxbase = wxbase;
var wxcloud = /** @class */ (function () {
    function wxcloud(uid) {
        this.uid = uid;
    }
    wxcloud.prototype.callFunction = function (json) {
        var data = json.data;
        var complete = json.complete;
        data['uid'] = this.uid;
        var json = Task[data.do](data);
        setTimeout(function () {
            complete(json);
        }, 200);
    };
    return wxcloud;
}());
var Task = {
    'joinRoom': joinRoom,
    'prepare': prepare,
    'prepareCard': prepareCard,
    'grabCard': grabCard,
    'playOut': playOut,
    'listen': listen,
    'quit': quit,
    null: function () { log('no task'); }
};
function listen(options) {
    return exports.db.collection('rooms').get(options.roomId);
}
function nextPlayer(roomInfo) {
    var playerKey = Object.keys(roomInfo.players);
    roomInfo.timeto = playerKey[(playerKey.indexOf(roomInfo.timeto) + 1) % 3];
}
function joinRoom(options) {
    var roomInfo = exports.db.collection('rooms').get(options.roomId);
    if (roomInfo) {
        roomInfo.players[options.uid] = false;
        roomInfo.playersInfo[options.uid] = exports.db.collection('users').get(options.uid);
        roomInfo.gameState = Object.keys(roomInfo.players).length <= 2 ? 'join' : 'prepare';
        exports.db.collection('rooms').doc(options.roomId).update(roomInfo);
        return roomInfo;
    }
    else {
        var players = {};
        players[options.uid] = false;
        var playersInfo = {};
        playersInfo[options.uid] = exports.db.collection('users').get(options.uid);
        roomInfo = {
            _id: options.roomId,
            gameState: 'join',
            players: players, playersInfo: playersInfo
        };
        exports.db.collection('rooms').add(roomInfo);
        return roomInfo;
    }
}
function prepare(options) {
    var roomInfo = exports.db.collection('rooms').get(options.roomId);
    roomInfo.players[options.uid] = true;
    var prepareCnt = 0;
    Object.keys(roomInfo.players).map(function (key) {
        if (roomInfo.players[key])
            prepareCnt++;
    });
    if (prepareCnt == 3) {
        Object.keys(roomInfo.players).map(function (key) {
            roomInfo.players[key] = false;
        });
        roomInfo.gameState = 'get';
        var cards = exports.pukeManeger.ShuffleCards();
        var bias = 0;
        roomInfo['hand'] = {};
        Object.keys(roomInfo.players).map(function (key) {
            roomInfo['hand'][key] = cards.slice(bias, bias = bias + 17);
        });
        roomInfo["dzcards"] = cards.slice(bias, cards.length);
    }
    exports.db.collection('rooms').doc(options.roomId).update(roomInfo);
    return roomInfo;
}
function prepareCard(options) {
    var roomInfo = exports.db.collection('rooms').get(options.roomId);
    roomInfo.players[options.uid] = true;
    var prepareCnt = 0;
    Object.keys(roomInfo.players).map(function (key) {
        if (roomInfo.players[key])
            prepareCnt++;
    });
    if (prepareCnt == 3) {
        roomInfo.gameState = 'grab';
        delete roomInfo.hand;
        delete roomInfo.dzcards;
        delete roomInfo.playersInfo;
        Object.keys(roomInfo.players).map(function (key) { roomInfo.players[key] = -1; });
        roomInfo['timeto'] = Object.keys(roomInfo.players)[lodash.shuffle([0, 1, 2])[0]];
    }
    exports.db.collection('rooms').doc(options.roomId).update(roomInfo);
    return roomInfo;
}
function grabCard(options) {
    var roomInfo = exports.db.collection('rooms').get(options.roomId);
    roomInfo.players[options.uid] = options.params.grab;
    var grabCnt = 0;
    Object.keys(roomInfo.players).map(function (key) {
        if (roomInfo.players[key] >= 0)
            grabCnt++;
    });
    nextPlayer(roomInfo);
    if (grabCnt == 3) {
        roomInfo['out'] = {};
        roomInfo.gameState = 'gaming';
        Object.keys(roomInfo.players).map(function (key) {
            roomInfo.players[key] = 17;
            roomInfo['out'][key] = {
                type: null, grade: -1, cards: []
            };
        });
        while (roomInfo.players[roomInfo.timeto] == 0)
            nextPlayer(roomInfo);
        roomInfo.players[roomInfo.timeto] += 3;
        roomInfo['lastOut'] = roomInfo.timeto;
        roomInfo['lastEffect'] = roomInfo.timeto;
    }
    exports.db.collection('rooms').doc(options.roomId).update(roomInfo);
    return roomInfo;
}
function playOut(options) {
    var cards = options.params.cards;
    var roomInfo = exports.db.collection('rooms').get(options.roomId);
    roomInfo.players[options.uid] -= cards.length;
    roomInfo.out[options.uid] = options.params;
    roomInfo.lastOut = options.uid;
    if (options.params.cards.length > 0)
        roomInfo.lastEffect = options.uid;
    if (roomInfo.players[options.uid] == 0)
        roomInfo.gameState = 'end';
    nextPlayer(roomInfo);
    exports.db.collection('rooms').doc(options.roomId).update(roomInfo);
    return roomInfo;
}
function quit(options) {
    var roomInfo = exports.db.collection('rooms').get(options.roomId);
    roomInfo.gameState = 'join';
    log('quit');
    delete roomInfo.players[options.uid];
    if (Object.keys(roomInfo.players).length == 0) {
        exports.db.collection('rooms').doc(options.roomId).remove();
    }
    exports.db.collection('rooms').doc(options.roomId).update(roomInfo);
    return roomInfo;
}
var puke = /** @class */ (function () {
    function puke() {
        this.value = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2', 'g', 'G'];
    }
    puke.prototype.grade = function (val) { return this.value.indexOf(val); };
    puke.prototype.ShuffleCards = function () {
        var _this = this;
        var unshuffledCards = [];
        var cardId = -1;
        this.value.map(function (value) {
            ['fk', 'hx', 'ht', 'mh'].map(function (type) {
                if (value != 'g' && value != 'G')
                    unshuffledCards.push({ value: value, type: type, grade: _this.grade(value),
                        color: (type == 'fk' || type == 'hx') ? 'RED' : 'BLACK',
                        text: value, id: cardId = cardId + 1 });
            });
        });
        var JOKER = 'J\nO\nK\nE\nR';
        unshuffledCards.push({ value: 'g', text: JOKER, type: null, grade: 13, color: 'BLACK', id: cardId = cardId + 1 });
        unshuffledCards.push({ value: 'G', text: JOKER, type: null, grade: 14, color: 'RED', id: cardId = cardId + 1 });
        var shuffledCards = lodash.shuffle(unshuffledCards);
        return shuffledCards;
    };
    return puke;
}());
exports.pukeManeger = new puke();

cc._RF.pop();